﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace InfoGenerateSystem.Component
{
    /// <summary>
    /// 文件操作Helper类
    /// 
    /// 创建人:     王昊
    /// 创建时间:   2007-11-07
    /// 
    /// </summary>
    public class FileHelper
    {
        private const int FO_DELETE = 0x3;
        private const ushort FOF_NOCONFIRMATION = 0x10;
        private const ushort FOF_ALLOWUNDO = 0x40;

        [DllImport("shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        private static extern int SHFileOperation([In, Out] _SHFILEOPSTRUCT str);

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
        private class _SHFILEOPSTRUCT
        {
            public IntPtr hwnd;
            public UInt32 wFunc;
            public string pFrom;
            public string pTo;
            public UInt16 fFlags;
            public Int32 fAnyOperationsAborted;
            public IntPtr hNameMappings;
            public string lpszProgressTitle;
        }

        public static Int32 DeleteToRecycleBin(String path)
        {
            _SHFILEOPSTRUCT pm = new _SHFILEOPSTRUCT();
            pm.wFunc = FO_DELETE;
            pm.pFrom = path + '\0';
            pm.pTo = null;
            pm.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
            return SHFileOperation(pm);
        }

        public static String ReadFileString(String fileName)
        {
            try
            {
                FileStream fs = File.OpenRead(fileName);
                Byte[] data = new Byte[fs.Length];
                fs.Read(data, 0, data.Length);
                fs.Close();

                return System.Text.UTF8Encoding.UTF8.GetString(data);
                //System.Text.Encoding gb = System.Text.Encoding.GetEncoding("gb2312");
                //return gb.GetString(data);
            }
            catch (FileNotFoundException)
            {
                throw new Exception("File not found.");
            }
        }

        public static void WriteFile(String fileName, String fileContent)
        {
            FileAttributes fa = FileAttributes.Normal;

            if (File.Exists(fileName))
            {
                FileInfo fiOriginal = new FileInfo(fileName);
                if (fiOriginal.Attributes.ToString().IndexOf("ReadOnly") != -1)
                {
                    fa = fiOriginal.Attributes;
                    fiOriginal.Attributes = FileAttributes.Normal;
                }
            }

            using (FileStream output = new FileStream(fileName, FileMode.Create))
            {
                Byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(fileContent);
                output.Write(data, 0, data.Length);
                output.Close();
            }

            FileInfo fiFinal = new FileInfo(fileName);
            fiFinal.Attributes = fa;
        }
    }
}